<?php

namespace app\admin\controller;

use app\admin\model\TaskRoomheatLog;
use app\admin\model\ZoneList;
use app\common\controller\Backend;
use think\Db;
use think\exception\DbException;
use think\Model;
use think\response\Json;

class Roomheat extends Backend
{
    /**
     * Anchor模型对象
     * @var \app\admin\model\Anchor
     */
    protected $model = null;
    protected $zone = null;
    protected $taskModel = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Roomheat();
        $this->taskModel = new TaskRoomheatLog();

        $this->zone = new ZoneList();

        $zoneList = $this->zone->all();
        $zoneData = [];
        foreach ($zoneList as $z) {
            $zoneData[$z->zone] = $z->zonename;
        }

        $this->assignconfig("zoneList", $zoneData);
    }

    /**
     * 查看
     *
     * @return string|Json
     * @throws \think\Exception
     * @throws DbException
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }
        //如果发送的来源是 Selectpage，则转发到 Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }
        [$where, $sort, $order, $offset, $limit] = $this->buildparams();

        $list = $this->model
            ->where($where)
            ->with("zoneList")
            ->order($sort, $order)
            ->paginate($limit);

        $result = ['total' => $list->total(), 'rows' => $list->items()];
        return json($result);
    }

    public function addjob()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }

        //设置过滤方法
        $params = $this->request->post('row/a');
        $must_param = [
            "livestreamerUser",
            "taskStarTime",
            'heatMinutes',
            'purposeOnlineNum'
        ];
        foreach ($must_param as $v){
            if(!isset($params[$v]) || empty($params[$v])){
                $this->error($v.'-是必传参数');
            }
        }
        $procedure_name = 'task_roomheat_add';
        $rs = $this->model->procedure_data_add($params,$must_param,$procedure_name);
        if($rs['res']){
            $this->success();
        }else{
            $this->error("操作失败");
        }
    }

    public function info()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }

        //如果发送的来源是 Selectpage，则转发到 Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }
        [$where, $sort, $order, $offset, $limit] = $this->buildparams();

        $list = $this->taskModel
            ->where($where)
            ->order($sort, $order)
            ->paginate($limit);

        $result = ['total' => $list->total(), 'rows' => $list->items()];
        return json($result);
    }

    public function searchUser()
    {
        $id = request()->get('id');

        $user = Db::table("user")->where([
            'username' => $id
        ])->find();

        if (!$user)
            $this->error("没有找到用户");

        $data = Db::table('user_right')->where([
            'livestreamerUser' => $id
        ])->find();

        if (!$data)
            $this->error("没有找到用户");



        $this->success("操作成功", null, [
            'grade' => $data['grade'],
            'fansSurplusNum' => $data['fansSurplusNum'],
            'fansNum' => $user['fansNum'],
        ]);
    }
}